/**
 * 
 */
package com.py.credit.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.AuScontent;
import com.py.credit.entity.SContentInfo;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.entity.enums.ApplicationTableFieldEnum;
import com.py.credit.entity.enums.AuScontentTableFieldEnum;
import com.py.credit.service.AuScontentService;
import com.py.credit.service.RemoteBaseService;
import com.py.credit.service.ScontentService;
import com.py.credit.service.ShareLogService;
import com.py.credit.utils.ExcelUtil;
import com.py.credit.utils.MapCache;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;

/**
 * @author dd
 *
 */
@RestController
@RequestMapping("/scontentAuthorization")
public class ScontentAuthorizationController {

	private final static String FRONT_TABLE_ID = "sauthorization";

	private final static String FRONT_TABLE_APPLICATION_ID = "application";

	@Autowired
	private AuScontentService auScontentService;

	@Autowired
	private ScontentService sContentService;

	@Autowired
	private ShareLogService shareLogService;

	@Autowired
	private RemoteBaseService remoteBaseService;

	@RequestMapping(value = "/add.json")
	public RestServiceResult<String> addAuthorizationScontent(AuScontent sa, HttpServletRequest request) {

		Long userId = null;

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {

				userId = userInfo.getId();
			}

		} else {

			ExceptionLogger.error("ContentController#commitCheck", "未获取到当前用户信息！");
		}

		auScontentService.add(sa, userId);

		return new RestServiceResult<String>();
	}

	@RequestMapping(value = "/update.json")
	public RestServiceResult<String> updateAuthorizationScontent(AuScontent sa) {

		auScontentService.update(sa);

		return new RestServiceResult<String>();
	}

	@RequestMapping(value = "/getDetail.json")
	public RestServiceResult<AuScontent> getDetail(
			@RequestParam(value = "id", required = true, defaultValue = "") String id) {

		return new RestServiceResult<AuScontent>(auScontentService.getAuScontentById(id));

	}

	@RequestMapping(value = "/del.json")
	public RestServiceResult<String> delAuthorizationScontent(
			@RequestParam(value = "ids", required = true, defaultValue = "") String ids) {

		auScontentService.del(ids);

		return new RestServiceResult<String>();
	}

	@RequestMapping(value = "/getScontentAuthorizationList.json")
	public RestServiceResult<List<Map<String, Object>>> getScontentAuthorizationList(
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
			HttpServletRequest request) {

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		String[] configColumns = null;

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {

				configColumns = (String[]) MapCache.get(userInfo.getId() + "_" + FRONT_TABLE_ID);

			} else {

				ExceptionLogger.error("ScontentAuthorizationController#getScontentAuthorizationList", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("ScontentAuthorizationController#getScontentAuthorizationList", "未获取到当前用户信息！");
		}

		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? AuScontentTableFieldEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);

		String[] additionalColumns = { "id" };

		List<Map<String, Object>> tableList = auScontentService.getByConditionForPage("P", showColumns,
				additionalColumns, condition, page);

		return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);

	}

	@RequestMapping(value = "/getOptionalColums.json")
	public RestServiceResult<List<OptionalColumn>> getOptionalColums() {

		return new RestServiceResult<List<OptionalColumn>>(AuScontentTableFieldEnum.getOptionalColumns());

	}

	@RequestMapping(value = "/getAdvancedConditionColumns.json")
	public RestServiceResult<List<OptionalColumn>> getAdvancedConditionColumns() {

		return new RestServiceResult<List<OptionalColumn>>(AuScontentTableFieldEnum.getAdvancedConditionColumns());

	}

	@RequestMapping(value = "/getApplicationOptionalColums.json")
	public RestServiceResult<List<OptionalColumn>> getApplicationOptionalColums() {

		return new RestServiceResult<List<OptionalColumn>>(ApplicationTableFieldEnum.getOptionalColumns());

	}

	@RequestMapping(value = "/getApplicationAdvancedConditionColumns.json")
	public RestServiceResult<List<OptionalColumn>> getApplicationAdvancedConditionColumns() {

		return new RestServiceResult<List<OptionalColumn>>(ApplicationTableFieldEnum.getAdvancedConditionColumns());

	}

	@RequestMapping(value = "/addApplicationShowColunms.json")
	public RestServiceResult<String> addApplicationShowColunms(
			@RequestParam(value = "columns", required = true) String[] columns, HttpServletRequest request) {

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {

				MapCache.set(userInfo.getId() + "_" + FRONT_TABLE_APPLICATION_ID, columns);

			} else {

				ExceptionLogger.error("ScontentAuthorizationController#addApplicationShowColunms", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("ScontentAuthorizationController#addApplicationShowColunms", "未获取到当前用户信息！");
		}

		return new RestServiceResult<String>();
	}

	@RequestMapping(value = "/addShowColunms.json")
	public RestServiceResult<String> addShowColunms(@RequestParam(value = "columns", required = true) String[] columns,
			HttpServletRequest request) {

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {

				MapCache.set(userInfo.getId() + "_" + FRONT_TABLE_ID, columns);

			} else {

				ExceptionLogger.error("ScontentAuthorizationController#addShowColunms", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("ScontentAuthorizationController#addShowColunms", "未获取到当前用户信息！");
		}

		return new RestServiceResult<String>();
	}

	@RequestMapping(value = "/getAuScontentStatistics.json")
	public RestServiceResult<Map<String, Integer>> getAuScontentStatistics() {

		SContentInfo sContentInfo = sContentService.getCountShareContentInfo("JKSQ");

		Map<String, Integer> map = new HashMap<String, Integer>();

		map.put("tableNum", sContentInfo == null ? 0 : sContentInfo.getColNum());
		map.put("categryNum", sContentInfo == null ? 0 : sContentInfo.getCategoryNum());
		map.put("deptNum", auScontentService.getAuScontentDeptNum());
		map.put("queryNum", shareLogService.getQueryCountByType(3));

		return new RestServiceResult<Map<String, Integer>>(map);

	}

	@RequestMapping(value = "/export.json")
	public void export(@RequestParam(value = "condition", defaultValue = "", required = true) String condition,
			@RequestParam(value = "columnEN", defaultValue = "", required = true) String columnEN, Page page,
			HttpServletResponse response) throws IOException {

		page.setLimit(1000);

		List<String> columns = JSONArray.parseArray(columnEN, String.class);

		List<Map<String, Object>> data = auScontentService.getByConditionForPage("P", columns, null, condition, page);
		List<OptionalColumn> optionalColumns = AuScontentTableFieldEnum.getOptionalColumns();

		ExcelUtil.downloadExcel(response, data, "外部共享授权列表", "外部共享授权列表", columns, optionalColumns);

	}

	@RequestMapping(value = "/getCurrentUserOffice.json")
	public RestServiceResult<String> getCurrentUserOffice(

			HttpServletRequest request) throws IOException {
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		String officeName = "";

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {

				officeName = userInfo.getOfficeName();

			} else {

				ExceptionLogger.error("ScontentAuthorizationController#getCurrentUserOffice", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("ScontentAuthorizationController#getCurrentUserOffice", "未获取到当前用户信息！");
		}

		return new RestServiceResult<String>(officeName);

	}

	@RequestMapping(value = "/getApplicationList.json")
	public RestServiceResult<List<Map<String, Object>>> getApplicationList(
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
			HttpServletRequest request) {

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

		String[] configColumns = null;

		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

			SysUser userInfo = user.getData();

			if (null != userInfo) {

				configColumns = (String[]) MapCache.get(userInfo.getId() + "_" + FRONT_TABLE_APPLICATION_ID);

			} else {

				ExceptionLogger.error("ScontentAuthorizationController#getApplicationList", "未获取到当前用户信息！");
			}

		} else {

			ExceptionLogger.error("ScontentAuthorizationController#getApplicationList", "未获取到当前用户信息！");
		}

		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? ApplicationTableFieldEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);

		String[] additionalColumns = { "id" };

		List<Map<String, Object>> tableList = auScontentService.getByConditionForPage(null, showColumns,
				additionalColumns, condition, page);

		return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);

	}

	@RequestMapping(value = "/applicationExport.json")
	public void applicationExport(
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition,
			@RequestParam(value = "columnEN", defaultValue = "", required = true) String columnEN, Page page,
			HttpServletResponse response) throws IOException {

		page.setLimit(1000);

		List<String> columns = JSONArray.parseArray(columnEN, String.class);

		List<Map<String, Object>> data = auScontentService.getByConditionForPage(null, columns, null, condition, page);
		List<OptionalColumn> optionalColumns = ApplicationTableFieldEnum.getOptionalColumns();

		ExcelUtil.downloadExcel(response, data, "外部共享授权列表", "外部共享授权列表", columns, optionalColumns);

	}

}
